<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="排序 从1开始  从小往大排序" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入排序 从1开始  从小往大排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="默认0 0正常； 1删除" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入默认0 0正常； 1删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:one:propetty:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:one:propetty:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:one:propetty:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:one:propetty:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="table" v-loading="loading" :data="propettyList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="排序" align="center" prop="source" />
      <el-table-column label="选择类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.chooseType == 1">多选</span>
          <span v-else>单选</span>
        </template>
      </el-table-column>
      <el-table-column label="属性类型" align="center">
        <template slot-scope="scope">
          <span v-for="item in typeList" v-if="item.type == scope.row.type">{{ item.label }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column type="expand" width="1">
        <template slot-scope="scope">
          <el-table ref="table2" :data="scope.row.sysTwoPropettyVModelList" :show-header=false class="secondTable"
            v-if="scope.row.sysTwoPropettyVModelList.length > 0" v-loading="scope.row.loading">
            <el-table-column align="center">
              <template slot-scope="scope2">
                <span>名称：{{ scope2.row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center">
              <template slot-scope="scope2">
                <span v-if="scope2.row.unit">单位：{{ scope2.row.unit }}</span>
              </template>
            </el-table-column>
            <!-- <el-table-column label="名称" align="center" prop="name" />
            <el-table-column label="单位" align="center" prop="unit" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope2">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope2.row, 2)"
                  v-hasPermi="['system:two:propetty:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope2.row, 2, scope.row)"
                  v-hasPermi="['system:two:propetty:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleSecondAdd(scope.row, 1)"
            v-hasPermi="['system:one:propetty:edit']">新增二级</el-button>
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleSecondShow(scope.row, 1)"
            v-hasPermi="['system:one:propetty:edit']">查看二级</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['system:one:propetty:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, 1)"
            v-hasPermi="['system:one:propetty:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改属性配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <template v-if="form.lableOneId">
          <el-form-item label="一级属性ID" prop="lableOneId">
            <el-input v-model="form.lableOneId" disabled placeholder="请输入一级属性主键ID" />
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="form.unit" placeholder="请输入单位" />
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="选择类型" prop="chooseType">
            <el-radio-group v-model="form.chooseType" width="100%">
              <el-radio :label="0">单选</el-radio>
              <el-radio :label="1">多选</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="排序" prop="source">
            <el-input v-model="form.source" placeholder="请输入排序 从1开始  从小往大排序" />
          </el-form-item>
          <!-- <el-form-item label="默认0 0正常； 1删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入默认0 0正常； 1删除" />
        </el-form-item> -->
          <el-form-item label="属性类型" prop="type">
            <el-select clearable v-model="form.type" placeholder="请选择属性类型">
              <el-option v-for="item in typeList" :value="item.type" :label="item.label" />
              <!-- <el-option :value="1" label="云上" />
            <el-option :value="2" label="IDC" />
            <el-option :value="3" label="全部" /> -->
            </el-select>
            <!-- <el-select clearable v-model="form.lineType" placeholder="请选择线路类型">
            <el-option v-for="(option, index) in idcList" :key="index" :value="option.dictValue"
              :label="option.dictLabel" />
          </el-select> -->
          </el-form-item>
          <!-- <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入编号" />
        </el-form-item> -->
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPropetty, listPagePropetty, getPropetty, delPropetty, addPropetty, updatePropetty } from "@/api/system/one/propetty";
import { listTwoPropetty, getTwoPropetty, delTwoPropetty, addTwoPropetty, updateTwoPropetty } from "@/api/system/two/propetty";

export default {
  name: "Propetty",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 属性配置表格数据
      propettyList: [],
      typeList: [{ type: 1, label: '云上' }, { type: 2, label: 'IDC' }, { type: 3, label: '全部' }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        source: null,
        chooseType: null,
        isDel: null,
        type: null,
        code: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        source: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        chooseType: [{ required: true, message: '选择类型不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '属性类型不能为空', trigger: 'blur' }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询属性配置列表 */
    getList() {
      this.loading = true;
      // listPropetty(this.queryParams).then(response => {
      // this.total = response.total;
      // let propettyList = response.rows || [];
      listPagePropetty(this.queryParams).then(response => {
        let propettyList = response.data || [];
        for (let i in propettyList) {
          propettyList[i].loading = false;
          // propettyList[i].sysTwoPropettyVModelList = [];
        }
        this.propettyList = propettyList;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        lableOneId: null,
        unit: null,
        source: null,
        chooseType: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        isDel: null,
        type: null,
        code: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加属性配置";
    },
    /** 新增f二级按钮操作 */
    handleSecondAdd(row) {
      this.reset();
      this.form.lableOneId = row.id;
      this.open = true;
      this.title = "添加二级属性配置";
    },
    /** 展示二级属性 */
    handleSecondShow(row) {
      let table = this.$refs['table'];
      table.toggleRowExpansion(row);
      this.getListTwoPropetty(row);
      // for (let m of this.propettyList) {
      //   if (m.id === row.id) {
      //     m.loading = true
      //     this.loading = true;
      //     let queryParams = {
      //       pageNum: 1,
      //       pageSize: 100,
      //       lableOneId: row.id,
      //     }
      //     listTwoPropetty(queryParams).then(response => {
      //       let sysTwoPropettyVModelList = response.rows || [];
      //       sysTwoPropettyVModelList.forEach(obj => {
      //         obj.loading = false;
      //         obj.sysTwoPropettyVModelList = [];
      //       });
      //       this.loading = false;
      //       m.sysTwoPropettyVModelList = sysTwoPropettyVModelList;
      //       if (sysTwoPropettyVModelList.length == 0) {
      //         this.$modal.msg('当前无下级信息')
      //       }
      //     });
      //     setTimeout(() => { m.loading = false }, 500)
      //     break
      //   }
      // }
    },
    getListTwoPropetty(row) {
      for (let m of this.propettyList) {
        if (m.id === row.lableOneId || m.id === row.id) {
          m.loading = true
          this.loading = true;
          let queryParams = {
            pageNum: 1,
            pageSize: 100,
            lableOneId: row.lableOneId || row.id,
          }
          listTwoPropetty(queryParams).then(response => {
            let sysTwoPropettyVModelList = response.rows || [];
            sysTwoPropettyVModelList.forEach(obj => {
              obj.loading = false;
              obj.sysTwoPropettyVModelList = [];
            });
            this.loading = false;
            m.sysTwoPropettyVModelList = sysTwoPropettyVModelList;
            if (sysTwoPropettyVModelList.length == 0) {
              this.$modal.msg('当前无下级信息')
            }
          });
          setTimeout(() => { m.loading = false }, 500)
          break
        }
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row, num) {
      this.reset();
      if (num == 1) {
        const id = row.id || this.ids
        getPropetty(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改属性配置";
        });
      } else {
        const id = row.id || this.ids
        getTwoPropetty(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改二级属性配置";
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lableOneId) {
            if (this.form.id != null) {
              updateTwoPropetty(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getListTwoPropetty(this.form);
                this.reset();
                // this.getList();
              });
            } else {
              addTwoPropetty(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getListTwoPropetty(this.form);
                this.reset();
                // this.getList();
              });
            }
          } else {
            if (this.form.id != null) {
              updatePropetty(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPropetty(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, num, data) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除属性配置名称为"' + row.name + '"的数据项？').then(function () {
        if (num == 2) {
          return delTwoPropetty(ids);
        } else {
          return delPropetty(ids);
        }
      }).then(() => {
        if (num == 2) {
          this.getListTwoPropetty(data);
        } else {
          this.getList();
        }
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/propetty/export', {
        ...this.queryParams
      }, `propetty_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.secondTable {
  padding-left: 15px;
}

.el-table__expand-icon {
  display: none;
}

.el-table__expanded-cell {
  padding: 0 !important;
  border: none !important;
}
</style>